#!/usr/bin/env python
#_*_ coding:utf-8 _*_
from django.shortcuts import render_to_response, redirect, HttpResponse
from app01 import models


# Create your views here.
def register(request):
    '''
    #创建用户类型
    t1=models.UserType.objects.create(name="超级管理员")
    t2=models.UserType.objects.create(name="普通管理员")
    #创建用户,user_type 必须是一个UserType对象
    u1=models.UserInfo.objects.create(username="张三",
                                      password='123',
                                      email='1@qq.com',
                                      user_type=t1)
    groupObjA=models.UserGroup.objects.create(groupname='用户组A')
    groupObjB=models.UserGroup.objects.create(groupname='用户组B')
    groupObjA.user.add(u1)
    groupObjB.user.add(u1)
    '''
    return HttpResponse('注册成功')
    
def login(request):
    ret={'status':'用户名或密码错误'}
    if request.method=='POST':
        username=request.POST.get('username',None)
        password=request.POST.get('password',None)
        is_auth=all([username,password])
        if is_auth:
            count=models.UserInfo.objects.filter(username=username,password=password).count()
            if count==1:
                #return redirect('/app01/index.html')
                ret['status']='验证通过，登录成功'
                return redirect('/app01/index',ret)
            else:
                ret['status']='用户名或 密码错误'
        else:
            ret['status']='用户名或密码为空'
    else:
        return render_to_response('app01/login.html',ret)
    return render_to_response('app01/login.html',ret)

def index(request):
    ret={'status':'index页面'}
    return render_to_response('app01/index.html',ret)

def host(request):
    ret={'status':'','data':None,'group':None}
    usergroup=models.UserGroup.objects.all()
    ret['group']=usergroup
    
    if request.method=='POST':
        hostname=request.POST.get('hostname',None)
        ip=request.POST.get('ip',None)
        groupId=request.POST.get('group',None)
        is_not_empty=all([hostname,ip])
        if is_not_empty:
            try:
                groupObj=models.UserGroup.objects.get(id=groupId)
                models.Asset.objects.create(hostname=hostname,
                                            ip=ip,
                                            user_group=groupObj)
                #raise Exception
                ret['status']='操作成功'
            except Exception:
                ret['status']='数据库操作异常，生产环境不应该显示该提示信息；应设置重试策略'
        else:
            ret['status']='您输入的hostname或ip为空，请重新输入！！！'
    else:
        pass
    assets = models.Asset.objects.all()
    #Query: SELECT `app01_asset`.`id`, `app01_asset`.`hostname`, `app01_asset`.`ip`, `app01_asset`.`user_group_id` FROM `app01_asset` INNER JOIN `app01_usergroup` ON (`app01_asset`.`user_group_id` = `app01_usergroup`.`id`) WHERE `app01_usergroup`.`groupname` = 用户组B
    
    #assets = models.Asset.objects.filter(user_group__id=1)
    print assets
    print assets.query
    ret['data']=assets
    return render_to_response('app01/host.html',ret)
def many2many(request):
    u1 = models.UserInfo.objects.get(id=1)
    g1=models.UserGroup.objects.get(id=1)
    #通过维护关联关系的一方添加
    #g1.user.add(u1)
    #通过被包含的一方添加：维护关系的一方的类名小写
    #u1.usergroup_set.add(g1)
    g1.user.remove(u1)
    return HttpResponse('many to many')

